﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SalesCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 反填退货通知单的所有信息处理方法
    /// </summary>
    public class FanTianreturn_notice_mainCommandHandler:IRequestHandler<FanTianreturn_notice_mainCommand,APIResult<List<Fanreturn_notice_mainDtos>>>
    {

        /// <summary>
        /// 开票通知单的仓储接口
        /// </summary>
        private readonly IBaseRepository<return_notice_main> rRepo;
        private readonly IBaseRepository<OrderMaterials> oRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iRepo"></param>
        public FanTianreturn_notice_mainCommandHandler(IBaseRepository<return_notice_main> rRepo, IBaseRepository<OrderMaterials> oRepo)
        {
            this.rRepo = rRepo;
            this.oRepo = oRepo;
        }

        public Task<APIResult<List<Fanreturn_notice_mainDtos>>> Handle(FanTianreturn_notice_mainCommand request, CancellationToken cancellationToken)
        {
            var list = from a in rRepo.GetAll().Result
                       join b in oRepo.GetAll().Result on a.document_code equals b.SaleCode
                       select new Fanreturn_notice_mainDtos
                       {
                           notice_id = a.notice_id,
                           document_code = a.document_code,
                           document_date = a.document_date,
                           business_type_id = a.business_type_id,
                           warehouse_id = a.warehouse_id,
                           operators = a.operators,
                           customer_name = a.customer_name,
                           document_status = a.document_status,
                           auditor = a.auditor,
                           audit_date = a.audit_date,
                           business_status_id = a.business_status_id,
                           return_reason_code = a.return_reason_code,
                           handling_method_code = a.handling_method_code,
                           remark = a.remark,
                           IsDel = a.IsDel,
                           ShenPiStatus = a.ShenPiStatus,

                           SaleCode = b.SaleCode,
                           MaterialsCode = b.MaterialsCode,
                           MaterialsName = b.MaterialsName,
                           ModelSpecification = b.ModelSpecification,
                           Calulate = b.Calulate,
                           CalulateNumber = b.CalulateNumber,
                           Price = b.Price,
                           TotalPrice = b.TotalPrice,
                           QualityRequir = b.QualityRequir,
                           PackRequir = b.PackRequir,
                           Batch = b.Batch,
                           Color = b.Color,
                           OutBoundNumber = b.OutBoundNumber,
                           Volume = b.Volume,
                           MaterialsClass = b.MaterialsClass,
                       };
            if (request.notice_id != null)
            {
                list = list.Where(X => X.notice_id == request.notice_id);
            }

            APIResult<List<Fanreturn_notice_mainDtos>> result = new APIResult<List<Fanreturn_notice_mainDtos>>();
            result.data = list.ToList();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.Count = list.Count();
            return Task.FromResult(result);
        }





    }



}
